package models

import (
	"blog_api/db"
)

type Category struct {
	Id         int    `gorm:"column(id)" json:"id"`
	ParentId   int    `gorm:"column(parent_id)" json:"parent_id"`
	Icon       string `gorm:"column(icon)" json:"icon"`
	CatName    string `gorm:"column(cat_name)" json:"cat_name"`
	Content    string `gorm:"column(content)" json:"content"`
	Order      int    `gorm:"column(order)" json:"order"`
	IsShow     int    `gorm:"column(is_show)" json:"is_show"`
	IsDel      int    `gorm:"column(is_del)" json:"is_del"`
	AddTime    uint64 `gorm:"column(add_time)" json:"add_time"`
	UpdateTime uint64 `gorm:"column(update_time)" json:"update_time"`
}

func (*Category) TableName() string {
	return "blog_category"
}

/**
 * CategoryTree
 * @Description: 首页栏目分类树
 * @Author: Jazk-Z
 * @return []map[string]interface{}
 */
func CategoryTree() []map[string]interface{} {
	var pp []Category
	db.Mysql.Model(&Category{}).Where("is_show = 1 and is_del = 0 and parent_id = 0").Order("`order`").Find(&pp)

	var cp []Category
	db.Mysql.Model(&Category{}).Where("is_show = 1 and is_del = 0 and parent_id > 0").Order("`order`").Find(&cp)

	var data []map[string]interface{}
	for _, v := range pp {
		item := make(map[string]interface{})
		item["id"] = v.Id
		item["icon"] = v.Icon
		item["cat_name"] = v.CatName
		item["order"] = v.Order
		var child []map[string]interface{}

		for _, vv := range cp {
			if vv.ParentId == v.Id {
				citem := make(map[string]interface{})
				citem["id"] = vv.Id
				citem["icon"] = vv.Icon
				citem["cat_name"] = vv.CatName
				citem["order"] = vv.Order
				child = append(child, citem)
			}
		}
		item["child"] = child
		data = append(data, item)
	}
	return data
}

type CatB struct {
	Id   int    `gorm:"column(id)" json:"id"`
	Name string `gorm:"column(name)" json:"name"`
}

/**
 * CatBreadcrumb
 * @Description: 首页面包屑
 * @Author: Jazk-Z
 * @param cat
 * @return []CatB
 */
func CatBreadcrumb(cid, lid int) []CatB {
	var catb []CatB
	cc := CatB{}
	cc.Id = 0
	cc.Name = "全部"
	if (cid + lid) == 0 {
		catb = append(catb, cc)
		return catb
	}
	if cid > 0 {
		var c Category
		db.Mysql.Model(&Category{}).Where("id", cid).Scan(&c)
		if c.ParentId == 0 {
			catb = append(catb, cc)

			cc.Id = c.Id
			cc.Name = c.CatName
			catb = append(catb, cc)
		} else {
			var ctmp Category
			db.Mysql.Model(&Category{}).Where("id", c.ParentId).Scan(&ctmp)
			cc.Id = ctmp.Id
			cc.Name = ctmp.CatName
			catb = append(catb, cc)

			cc.Id = c.Id
			cc.Name = c.CatName
			catb = append(catb, cc)
		}
		return catb
	}
	if lid > 0 {
		catb = append(catb, cc)

		var l Label
		db.Mysql.First(&l, lid)
		cc.Id = l.Id
		cc.Name = l.LabelName
		catb = append(catb, cc)
		return catb
	}
	return catb

}

func AdminCategoryList(limit, offset int, field, keyword string) ([]Category, int64) {
	var a []Category
	var c int64
	l := db.Mysql.Model(&Category{})
	if keyword != "" {
		ids, err := db.SearchKeyword(keyword, ES_CATEGORY_INDEX)
		if err != nil {
			return a, 0
		}
		l.Where("id in (?)", ids)
	}
	l.Count(&c)
	l.Select(field).Order("add_time desc").Limit(limit).Offset(offset).Scan(&a)
	return a, c
}
